package neu.soft.service.impl;

import neu.soft.feignclient.FindInboundRoomId;
import neu.soft.mapper.OrderInquireMapper;
import neu.soft.query.WarehousePageQuery;
import neu.soft.service.OrderInquireService;
import neu.soft.vo.PageBean;
import neu.soft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class OrderInquireServiceImpl implements OrderInquireService {
    @Autowired
    private OrderInquireMapper orderInquireMapper;
    @Autowired
    private FindInboundRoomId findInboundRoomId;

    @Override
    public ResultBean<PageBean<Map<String,Object>>> findOrder(Map<String,Object> map) {
        ResultBean<PageBean<Map<String,Object>>> re = new ResultBean<>();
        PageBean<Map<String, Object>> pageBean=new PageBean<>();
        int pageNow = (int) map.get("pageNow");
        int pageSize = (int) map.get("pageSize");
        if(map.get("inboundRoomId")!=null){
//            System.out.println(map.get("inboundRoomId"));
//            System.out.println(this.findIdByProvince((String) map.get("inboundRoomId")));
            map.put("inboundRoomId",this.findIdByProvince((String) map.get("inboundRoomId")));
        }
        List<Map<String, Object>>list=orderInquireMapper.findOrder(map);
        for (int i=0;i<list.size();i++){
            Date date= (Date) list.get(i).get("arriveTime");
            if(date!=null){
                SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
                String s2 = sdf2.format(date);
                list.get(i).put("arriveTime",s2);
            }
        }
        Long totalRows= orderInquireMapper.getTotalRows(map);
        pageBean.setRowData(list);
        pageBean.setTotalRows(totalRows);
        pageBean.setTotalPages((int) (totalRows % pageSize == 0 ? (totalRows/pageSize) : (totalRows/pageSize + 1)));
        re=re.success(pageBean);
        return re;
    }

    @Override
    public ResultBean<Map<String, Object>> findPrintOrder(String orderId) {
        ResultBean<Map<String,Object>> re=new ResultBean<>();
        Map<String,Object>map=orderInquireMapper.findPrintOrder(orderId);
        re=re.success(map);
        return re;
    }

    @Override
    public ResultBean<PageBean<Map<String, Object>>> findUndistributedOrder(Map<String, Object> map) {
        ResultBean<PageBean<Map<String,Object>>> re = new ResultBean<>();
        PageBean<Map<String, Object>> pageBean=new PageBean<>();
        int pageNow = (int) map.get("pageNow");
        int pageSize = (int) map.get("pageSize");
        List<Map<String, Object>>list=new ArrayList<>();
        list=orderInquireMapper.findUndistributedOrder(map);
        Long totalRows= orderInquireMapper.getTotal(map);
        pageBean.setRowData(list);
        pageBean.setTotalRows(totalRows);
        pageBean.setTotalPages((int) (totalRows % pageSize == 0 ? (totalRows/pageSize) : (totalRows/pageSize + 1)));
        re=re.success(pageBean);
        return re;
    }

    @Override
    public ResultBean<List<Map<String, Object>>> findShopping(String orderId) {
        ResultBean<List<Map<String,Object>>> re=new ResultBean<>();
        List<Map<String,Object>>map=orderInquireMapper.findShopping(orderId);
        re=re.success(map);
        return re;
    }

    @Override
    public ResultBean<List<Map<String, Object>>> findTransfer(String orderId) {
        ResultBean<List<Map<String,Object>>> re=new ResultBean<>();
        System.out.println(orderId);
        List<Map<String,Object>>list=orderInquireMapper.findTransfer(orderId);
        System.out.println(list);
        re=re.success(list);
        return re;
    }
    private int findIdByProvince(String warehouseAddress){
        WarehousePageQuery warehouseQuery=new WarehousePageQuery();
        warehouseQuery.setWarehouseAddress(warehouseAddress);
        return findInboundRoomId.findByPage(warehouseQuery).getData().getRowData().get(0).getWarehouseId();
    }
}
